import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../../layout/index.vue'
import Fors from '../views/404.vue'
import store from '../store'
import filterRouters from '@/utils/filterRouters'

Vue.use(VueRouter)


 const staticRoutets = [
  {
    path: '/',
    name: 'home',
    component: Layout,
    meta: {
      title: '首页',
      hidden: true,

    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      title: '登录',
      hidden: true
    }

  },
  {
    path: '*',
    component: Fors,
    meta: {
      title: '404',
      hidden: true
    }
  }
]

export const asyncRoutes = [
    {
      path: '/personnel',
      name: 'personnel',
      component: Layout,
      meta: {
        title: "推荐专场轮播",
      },
      redirect: "/personnel/personnel",
      children: [
        {
          path: "/personnel/personnel",
          name: 'personnel_personnel',
          component: () => import('@/views/banner/index.vue'),
          meta: {
            title: "推荐轮播管理",
          }
        }
      ]
      },


      {
        path: '/user',
        name: 'user',
        component: Layout,
        meta: {
          title: "账号管理",
        },
        redirect: "/user/user",
        children: [
          {
            path: "/user/user",
            name: 'user_user',
            component: () => import('@/views/user/index.vue'),
            meta: {
              title: "管理员",
            }
          },
          {
            path: "/user/userto",
            name: 'user_userto',
            component: () => import('@/views/user/user.vue'),
            meta: {
              title: "用户表",
            }
          },
        ]
        },

        {
          path: '/business',
          name: 'business',
          component: Layout,
          meta: {
            title: "门店管理",
          },
          redirect: "/business/business",
          children: [
            {
              path: "/business/business",
              name: 'business_business',
              component: () => import('@/views/business/index.vue'),
              meta: {
                title: "门店列表",
              }
            }
          ]
          },
          {
            path: '/product',
            name: 'product',
            component: Layout,
            meta: {
              title: "产品管理",
            },
            redirect: "/product/product",
            children: [
              {
                path: "/product/prolist",
                name: 'product_product',
                component: () => import('@/views/product/prolist.vue'),
                meta: {
                  title: "商品列表",
                }
              },
            ]
            },
            {
              path: '/classifier',
              name: 'classifier',
              component: Layout,
              meta: {
                title: "类别管理",
              },
              redirect: "/classifier/classifier",
              children: [
                {
                  path: "/classifier/classifier",
                  name: 'classifier_classifier',
                  component: () => import('@/views/classifier/index.vue'),
                  meta: {
                    title: "商品分类",
                  }
                }
              ]
              },
            
                {
                  path: '/tablemap',
                  name: 'tablemap',
                  component: Layout,
                  meta: {
                    title: "大数据",
                  },
                  redirect: "/tablemap/tablemap",
                  children: [
                    {
                      path: "/tablemap/tablemap",
                      name: 'tablemap_tablemap',
                      component: () => import('@/views/tablemap/index.vue'),
                      meta: {
                        title: "数据可视",
                      }
                    }
                  ]
                  },
                  {
                    path: '/delicacy',
                    name: 'delicacy',
                    component: Layout,
                    meta: {
                      title: "美食清单",
                    },
                    redirect: "/delicacy/delicacy",
                    children: [
                      {
                        path: "/delicacy/delicacy",
                        name: 'delicacy_delicacy',
                        component: () => import('@/views/delicacy/cacy.vue'),
                        meta: {
                          title: "新增美食",
                        }
                      }
                    ]
                    },
        
]
let routes = [...staticRoutets,...asyncRoutes]
let st =store.state.login.profile
const router = new VueRouter({
  // routes
  routes
})
let routerdata= true
router.beforeEach((to, from, next) => {
  // 更改网页标题
  document.title = to.meta.title
  // token值
  let token = store.state.login.profile.token.length>10
  //权限数组
  if (token || to.path === '/login') {

    if(routerdata){
      let fil = filterRouters(asyncRoutes, store.state.login.profile.checkedkeys)
      fil.forEach(item => {
        router.addRoute(item)
      })
      routerdata = false
     
      next(to.path)
    }
    next()
  } else {
      next('/login?redirect=' + to.path)
  }
})

export default router
